package com.sys.ai.controller;

import com.sys.ai.domain.AiApps;
import com.sys.ai.service.IAiAppsService;
import com.sys.common.annotation.Log;
import com.sys.common.core.controller.BaseController;
import com.sys.common.core.domain.AjaxResult;
import com.sys.common.core.page.TableDataInfo;
import com.sys.common.enums.BusinessType;
import com.sys.common.utils.poi.ExcelUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * AI应用Controller
 *
 * @author LGY
 * @date 2025-05-19
 */
@Tag(name = "AI应用")
@RestController
@RequestMapping("/ai/aiApps")
public class AiAppsController extends BaseController {
    @Autowired
    private IAiAppsService aiAppsService;

    /**
     * 查询AI应用列表
     */
    @PostMapping("/list")
    @Operation(summary = "查询AI应用分页列表")
    public TableDataInfo list(@RequestBody AiApps aiApps) {
        startPage(aiApps);
        List<AiApps> list = aiAppsService.selectAiAppsList(aiApps);
        return getDataTable(list);
    }

    @PostMapping("/listAllRelease")
    @Operation(summary = "查询所有已发布APP")
    public AjaxResult listAllReleaseApps(@RequestBody AiApps aiApps) {
        List<AiApps> list = aiAppsService.listAllReleaseApps(aiApps);
        return success(list);
    }

    /**
     * 导出AI应用列表
     */
    @Log(title = "AI应用", businessType = BusinessType.EXPORT)
    @Operation(summary = "导出AI应用")
    @PostMapping("/export")
    public void export(HttpServletResponse response, AiApps aiApps) {
        List<AiApps> list = aiAppsService.selectAiAppsList(aiApps);
        ExcelUtil<AiApps> util = new ExcelUtil<AiApps>(AiApps.class);
        util.exportExcel(response, list, "AI应用数据");
    }

    /**
     * 获取AI应用详细信息
     */
    @Operation(summary = "查询AI应用详情")
    @GetMapping(value = "/detail/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return success(aiAppsService.selectAiAppsById(id));
    }

    /**
     * 新增AI应用
     */
    @Log(title = "AI应用", businessType = BusinessType.INSERT)
    @Operation(summary = "新增AI应用")
    @PostMapping("/save")
    public AjaxResult add(@RequestBody AiApps aiApps) {
        aiAppsService.insertAiApps(aiApps);
        return success();
    }

    /**
     * 修改AI应用
     */
    @Log(title = "AI应用", businessType = BusinessType.UPDATE)
    @Operation(summary = "修改AI应用")
    @PutMapping("/update")
    public AjaxResult edit(@RequestBody AiApps aiApps) {
        aiAppsService.updateAiApps(aiApps);
        return success();
    }

    /**
     * 删除AI应用
     */
    @Operation(summary = "删除AI应用")
    @Log(title = "AI应用", businessType = BusinessType.DELETE)
    @DeleteMapping("/remove/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        aiAppsService.deleteAiAppsByIds(ids);
        return success();
    }

    @Operation(summary = "更新AI应用状态")
    @Log(title = "AI应用", businessType = BusinessType.UPDATE)
    @PutMapping("/updateStatus/{id}/{status}")
    public AjaxResult updateStatus(@PathVariable("id") String id, @PathVariable("status") Integer status) {
        AiApps aiApps = aiAppsService.getById(id);
        aiApps.setStatus(status);
        aiAppsService.updateById(aiApps);
        return success();
    }
}